package com.ch.light.web.controller;

import com.ch.light.web.model.Dict;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;

import com.ch.light.core.Response;
import com.ch.light.core.page.Page;
import com.ch.light.mybatis.ParamFilter;
import com.ch.light.web.aop.annotaion.WebLogger;
import com.ch.light.web.service.DictService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.annotation.Resource;

import static com.google.common.base.Preconditions.checkArgument;


@Controller
@RequestMapping("dict")
public class DictController {

    @Resource
    private DictService dictService;

    @GetMapping("list")
    public String list() {
        return "dictList";
    }

    @ResponseBody
    @PostMapping("list")
    @WebLogger("查询字典列表")
    public Response list(@RequestBody ParamFilter queryFilter) {
        List<Dict> records = dictService.findPageList(queryFilter);
        PageInfo<Dict> pageInfo = new PageInfo<>(records);
        Page page = queryFilter.getPage();
        page.setResultCount(Long.valueOf(pageInfo.getTotal()).intValue());
        return new Response(records, queryFilter.getPage());
    }

    @ResponseBody
    @PostMapping("add")
    @WebLogger("添加字典")
    public Object add(@RequestBody Dict dict) {
        Preconditions.checkNotNull(dict, "不能为空");
        Response response = new Response();
        if (dict.getId() == null) {
            dictService.save(dict);
        } else {
            dictService.update(dict);
        }
        response.setMsg("添加成功");
        return response;
    }

    @ResponseBody
    @GetMapping("detail")
    @WebLogger("查询字典详细")
    public Response detail(Long id) {
        Preconditions.checkNotNull(id, "不能为空");
        Dict dict = dictService.getById(id);
        return new Response(dict);
    }


    @ResponseBody
    @PostMapping("delete")
    @WebLogger("删除字典")
    public Response delete(@RequestBody List<Long> ids) {
        checkArgument((ids != null && ids.size() > 0), "不能为空");
        dictService.delete(ids);
        return new Response();
    }

    @ResponseBody
    @GetMapping("getTree")
    public Object getTree() {
        return dictService.getTree();
    }

    @ResponseBody
    @GetMapping("getByParentId")
    public Response getByParentId(Long parentId) {
        List<Dict> dictList = dictService.findById(parentId);
        return new Response(dictList);
    }

    @ResponseBody
    @GetMapping("getCategory")
    public Response getCategory() {
        Dict record = new Dict();
        record.setIsCategory("Y");
        List<Dict> list = dictService.find(record);
        Response response = new Response();
        response.setData(list);
        return response;
    }
}
